//
//  CityViewController.m
//  didacar
//
//  Created by 度周末网络-王腾 on 15/11/27.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "CityViewController.h"
#import "CityCell.h"
#import "NSUserDefaults+Addition.h"
#import "GpsManager.h"
#import "CityViewModel.h"
#import "CityAreaModel.h"

@interface CityViewController ()

@property (nonatomic,strong) CityViewModel *cityViewModel;

@property (weak, nonatomic) IBOutlet UITableView *mainTable;

// 定位城市
@property (weak, nonatomic) IBOutlet UIView *gpsCityView;

// 其他城市
@property (weak, nonatomic) IBOutlet UIView *otherCityView;

// 正在定位
@property (nonatomic, assign) BOOL isGPSing;

@end

@implementation CityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.cityViewModel = [[CityViewModel alloc ] init];
    [self.cityViewModel getCityAreaList:^(NSInteger resultType, id data, NSString *errorMsg) {
        if (resultType == 1) {
            [self.mainTable reloadData];
        }
    }];
    
    [self setLeftCloseBtnItem];
    
    [self setExtraCellLineHidden:self.mainTable];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gpsSuccess) name:GPSSuccess object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gpsFailure) name:GPSFailure object:nil];
    
    
    
}

-(void)gpsSuccess{
    self.isGPSing = NO;
    [self.mainTable reloadData];
}

-(void)gpsFailure{
    self.isGPSing = NO;
    [self.mainTable reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 35;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    return self.cityViewModel.resultData.count;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return self.gpsCityView;
    }
    return self.otherCityView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        NSString *city = [[NSUserDefaults standardUserDefaults] getUserSelectedCity];
        if (city == nil) {
            self.isGPSing = YES;
            [[GpsManager shareManager] startGPS];
            [self.mainTable reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
        }
    }else{
        CityAreaModel *cityModel = self.cityViewModel.resultData[indexPath.row];
        
        [[NSUserDefaults standardUserDefaults] setUserSelectedCity:cityModel.CityName];
        [[NSUserDefaults standardUserDefaults] setUserSelectedCityID:cityModel.CityId];
        [[NSNotificationCenter defaultCenter] postNotificationName:ResetLocation object:nil];
        [self dismissViewControllerAnimated:YES completion:NULL];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CityCell *cityCell = [tableView dequeueReusableCellWithIdentifier:@"CityCell"];
    if (indexPath.section == 0) {
        
        NSString *city = [[NSUserDefaults standardUserDefaults] getUserSelectedCity];
        if (city == nil) {
            if (self.isGPSing) {
                cityCell.cityName.text = @"正在定位..";
            }
            else{
                cityCell.cityName.text = @"定位失败，点击重试";
            }
            
        }else{
            cityCell.cityName.text = city;
        }
    }else{
        
        CityAreaModel *cityModel = self.cityViewModel.resultData[indexPath.row];
        cityCell.cityName.text = cityModel.CityName;
    }
    return cityCell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
}

@end
